<cfparam default="none" name="url.api" />


<!---Need to call BASE functions--->
<cfset BASE = CreateObject("component", "#session.rootDir#.cfc.base") />
<cfset baseQueries = CreateObject("component", "#session.rootDir#.cfc.queries") />
<cfset step1Queries = CreateObject("component", "#session.rootDir#.cfc.step1Queries") />
<cfset termQueries = CreateObject("component", "#session.rootDir#.cfc.terminationQueries") />
<cfinclude template="/code_lib/SCF_Employee.cfm">
<cfinclude template="/code_lib/SCF_HR.cfm">
<cfinclude template="/code_lib/SCF_REGEX.cfm">

<cfoutput>

<!--- API that handles step1 Termination Function: funcInsertAction --->
<cfif url.api eq "step1Termination_insert" >    
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST" >            
            <cfset termQuereis = CreateObject("component", "#session.rootDir#.cfc.terminationQueries") />
            <cfset TermReqID = termQuereis.funcInsertTermanationRequestAction(FORM) />            
            <cfset baseQueries.funcInsertAction("", 18 , Session.intUserRosettaID, '#FORM.Comments#', "True", TermReqID) />
            <cfset baseQueries.funcInsertAction("", 20 , "", "", "", TermReqID) />
            <cfif FORM.FILEUPLOAD NEQ "">
               <cffile action='upload' filefield='FileUpload' destination='#FORM.Directory#' nameconflict='makeunique' />
               <cfset AddFile = termQuereis.funcInsertFile(-1, TermReqID, '#cffile.ServerFile#', Form.FileTypeID, Session.intUserRosettaID ) /> 
            </cfif>            
            <cfset Session.EmpConfirm = "The request for termination of the employee " & 
            SCF_Employee_GetEmployee(FORM.RosettaID).FullName & 
            " has been successfully submitted to HR.  Thank you for using E-PAR.
            <p>Initiate another request?  Click <a href=""Step1-Request.cfm"">Here</a>">            
            <cflocation url="Step1-Confirmation.cfm" addtoken="no">
            
        </cfcase>    
    </cfswitch>
</cfif>
<!--- API that handles step1 Termination Function: funcInsertAction --->




<!--- API that handles step2 Termination Acknowledgement Function:  --->
<cfif url.api eq "step2Termination_acknowledgement" >    
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST" >            
            <cfset Session.TermReqID = Form.TermReqID>
            <cflocation url="Step2-HRAcknowledgeRequest2.cfm?TermReqID=#Session.TermReqID#" addtoken="no">            
        </cfcase>    
    </cfswitch>
</cfif>
<!--- API that handles step2 Termination Acknowledgement Function:  --->




<!--- API that handles step2 rejection of termination  --->
<cfif url.api eq "step2Termination_reject_term" >    
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST" >
            <cfset termDetails = baseQueries.funcGetAllTermDetails(Form.TermReqID) />
            <cfset Session.TermReqID = Form.TermReqID />
            <cfset Supervisor = SCF_HR_WhosTheBoss(TermDetails.RosettaID) />
            <cfset Supervisor = SCF_EMPLOYEE_GetEmployee(Supervisor) />
            <cfset Rejector = SCF_EMPLOYEE_GetEmployee(TermDetails.ActionByID) />
            <cfset RejectionDetails = "Termination step 2 rejected on #DateFormat(now(), 'm/d/yyyy')# by #TermDetails.ActionByID#.  Rejector comments: #FORM.Comments#" />            
            <cfset baseQueries.funcInsertErrorLog(TermDetails.ActionStepID, '#Session.intUserRosettaID#', RejectionDetails) />            
            <cfset DeleteStepActions = termQueries.funcDeleteTermanationStepAction(Form.TermReqID) />
            <cfset Alert = CreateObject("component", "#session.rootDir#.cfc.Termination").init() />
            <cfset Alert.AlertEmail( "Termination request denied by HR.", "Termination request rejected on #DateFormat(now(), 'm/d/yyyy')# by #Rejector.FullName#.  Rejector comments: #FORM.Comments#", 
	            "EPAR_Termination@scf.cc", 
	            "#Supervisor.Email#") />
            <cflocation url="Status.cfm" addtoken="no">            
        </cfcase>    
    </cfswitch>
</cfif>
<!--- API that handles step2 rejection of termination  --->




<!--- API that handles step2 Termination Database update Function:  --->
<cfif url.api eq "step2Termination_acknowledgement_update" >    
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST" >            
            <cfset termQuereis = CreateObject("component", "#session.rootDir#.cfc.terminationQueries") />            
            <cfset UpdateTerminationRequest = termQuereis.funcUpdateTermanationRequestAction(FORM) />            
            <cflocation url="Step2-HRAcknowledgeRequest3.cfm" addtoken="no">
        </cfcase>    
    </cfswitch>
</cfif>
<!--- API that handles step2 Termination Database update Function:  --->




<!--- API that handles step2 Termination Acknoledge Request 3 uploading a file Function:  --->
<cfif url.api eq "step2Termination_acknowledgement_file_upload" >    
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST" >
            
            <cfset termQuereis = CreateObject("component", "#session.rootDir#.cfc.terminationQueries") />                                       
	        <cffile action='upload' filefield='FileUpload' destination='#FORM.Directory#' nameconflict='makeunique' />	           
	        <cfset AddFile = termQuereis.funcInsertFile(-1, FORM.TermReqID, '#cffile.ServerFile#', Form.FileTypeID, Session.intUserRosettaID ) />                                    
            <cflocation url="#CGI.HTTP_REFERER#" addtoken="no" />
        </cfcase>    
    </cfswitch>
</cfif>
<!--- API that handles step2 Termination Acknoledge Request 3 uploading a file Function:  --->




<!--- API that handles step2 Termination Acknoledge Request 3 finish Function:  --->
<cfif url.api eq "step2Termination_acknowledgement_finish" >    
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST" >        
	        <cfset QUERIES.GetTermDetails = baseQueries.funcGetTermDetails( FORM.TermReqID, 20 ) />	        
	        <cfset UpdateAction = baseQueries.funcUpdateAction( QUERIES.GetTermDetails.ActionStepID, Session.intUserRosettaID, FORM.Comments ) />	        
	        <cfset InsertAction = baseQueries.funcInsertAction("", 22, "", "", "", FORM.TermReqID ) />	         
	        <cflocation url="Step3-ExecTerm.cfm?TermReqID=#FORM.TermReqID#" addtoken="no"> 
        
        </cfcase>
    </cfswitch>
</cfif>
<!--- API that handles step2 Termination Acknoledge Request 3 finish Function:  --->




<!--- API that handles SCF Step4 finance termination: --->
<cfif url.api eq "step4_finance_manager_termination" >
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST" >
            <cfloop index="i" list="#FORM.FieldNames#" DELIMITERS=",">
	            <cfif left(i,8) EQ "CostCode" AND FORM[i] NEQ "">
				    <cfset LOCALS.StepActionID = right( ListGetAt(i, 1, ":"), len(ListGetAt(i, 1, ":"))-8) />
					<cfset LOCALS.TermReqID = ListGetAt(i, 2, ":") />
					<cfset LOCALS.CostCode = FORM[i] />
					<cfset UpdateCostCode = termQueries.funcUpdateCostCode( LOCALS.TermReqID, LOCALS.CostCode ) />
                    <cfset UpdateAction = baseQueries.funcUpdateAction( LOCALS.StepActionID, Session.intUserRosettaID, 'Completed by Finance' ) />
					<cfset InsertAction = baseQueries.funcInsertAction("", 25, "", "", "", LOCALS.TermReqID ) />
	            </cfif>
            </cfloop>
            <cflocation url="status.cfm" addtoken="no" />
        </cfcase>    
    </cfswitch>
</cfif>
<!--- API that handles SCF Step4 finance termination: --->





<!--- API that handles SCF Step4 finance termination: --->
<cfif url.api eq "step4_finance_termination" >
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST" >
            <cfloop index="i" list="#FORM.FieldNames#" DELIMITERS=",">
                <cfif left(i,4) EQ "Term">
                    <cfset LOCALS.StepActionID = right(i, len(i)-4) />
                    <cfset LOCALS.TermReqID = FORM[i] />
                    <cfset QUERIES.UpdateAction = baseQueries.funcUpdateAction( LOCALS.StepActionID,Session.intUserRosettaID, 'Completed by Finance' ) />
                    <cfset InsertAction = baseQueries.funcInsertAction("", 26, "", "", "", LOCALS.TermReqID ) />
                </cfif>
            </cfloop>
            <cflocation url="status.cfm" addtoken="no" />        
        </cfcase>    
    </cfswitch>
</cfif>
<!--- API that handles SCF Step4 finance termination: --->



<!--- API that handles SCF Step5 payroll termination: --->
<cfif url.api eq "step5_payroll_termination" >
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST" >
            <cfloop index="i" list="#FORM.FieldNames#" DELIMITERS=",">
                <cfif left(i,4) EQ "Term">
                    <cfset LOCALS.StepActionID = right(i, len(i)-4) />
                    <cfset LOCALS.TermReqID = FORM[i] />
                    <cfset QUERIES.UpdateAction = baseQueries.funcUpdateAction( LOCALS.StepActionID, Session.intUserRosettaID, 'Completed by Finance' ) />
                    <cfset InsertAction = baseQueries.funcInsertAction("", 27, "", "", "", LOCALS.TermReqID ) />
                </cfif>
            </cfloop>
            <cflocation url="status.cfm" addtoken="no" />        
        </cfcase>    
    </cfswitch>
</cfif>
<!--- API that handles SCF Step5 payroll termination: --->




<!--- API that handles SCF Step5 access_control termination: --->
<cfif url.api eq "step6_access_control_termination" >
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST" >
	        <cfloop index="i" list="#FORM.FieldNames#" DELIMITERS=",">
	            <cfif left(i,4) EQ "Term">
	                <cfset LOCALS.StepActionID = right(i, len(i)-4) >
                    <cfset LOCALS.TermReqID = FORM[i] />	                
                    <cfset QUERIES.UpdateAction = baseQueries.funcUpdateAction( LOCALS.StepActionID, Session.intUserRosettaID, 'Completed by Facilites' ) />
                    <cfset InsertAction = baseQueries.funcInsertAction("", 28, "", "", "", LOCALS.TermReqID ) />
	            </cfif>
	        </cfloop>                                               
            <cflocation url="status.cfm" addtoken="no" />            
        </cfcase>    
    </cfswitch>
</cfif>
<!--- API that handles SCF Step5 access_control termination: --->



<!--- API that handles user change to Termination date: --->
<cfif url.api eq "step3_scheduleterm_date_update" >
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST" >
            <cfdump var="#FORM#" />
            <cfset termQueries.funcUpdateTermanationDates(FORM) />
        </cfcase>    
    </cfswitch>
</cfif>
<!--- API that handles user change to Termination date: --->


</cfoutput>